﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GrayParrot.Configuration;
using GrayParrot.RabbitMQ.Configuration;
using GrayParrot.Scheduling;
using Newtonsoft.Json.Linq;

namespace TestRabbitMQ
{
    [Serializable]
    public class MessaggioTest
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public MessaggioTest()
        {

        }
    }

    class Program
    {
        public static bool IsNullOrEmpty(JToken token)
        {
            return (token == null) ||
                   (token.Type == JTokenType.Array && !token.HasValues) ||
                   (token.Type == JTokenType.Object && !token.HasValues) ||
                   //(token.Type == JTokenType.Property && !token.HasValues) ||
                   (token.Type == JTokenType.String && token.ToString() == String.Empty) ||
                   (token.Type == JTokenType.Null);
        }

        static void Main(string[] args)
        {
            JObject person = JObject.Parse(@"{
              'name': 'James',
              'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'],
              'altro': ''
            }");

            var objs = person.Children();
            foreach (JToken t in objs)
            {
                if (Check(t))
                {
                    Console.WriteLine(t);
                }
            }

            Console.WriteLine(objs);

            //MessaggioTest m_test = new MessaggioTest();
            //m_test.Name = "pippo";
            //m_test.Age = 40;


            //SendMessageTest();

            //ReadMessageTest();

            ScheduleMessageTest();
        }

        private static bool Check(JToken token)
        {
            bool retValue = false;

            if (token.HasValues)
            {
                foreach (JToken t in token.Values())
                {
                    retValue = IsNullOrEmpty(t);
                }
            }

            return retValue;
        }

        private static void SendMessageTest()
        {
            ConfigurationManagerHelper pp = new ConfigurationManagerHelper();
            GrayParrot.RabbitMQ.Configuration.Subscription m_sub = pp.GetSubscription("GrayParrot-Test");

            GrayParrot.RabbitMQ.Publisher<MessaggioTest> m_pub = new GrayParrot.RabbitMQ.Publisher<MessaggioTest>(m_sub);
            for (int i = 0; i < 1000; i++)
            {
                MessaggioTest m_test = new MessaggioTest();
                m_test.Name = "pippo " + i;
                m_test.Age = 40;

                bool result = m_pub.Send(new GrayParrot.RabbitMQ.MessageBusBase<MessaggioTest>() { Body = m_test });
            }
            //Console.WriteLine(result);
        }

        private static void ReadMessageTest()
        {
            RabbitMQSubscriber m_tsb = new RabbitMQSubscriber();
            m_tsb.Init();
            //m_tsb.Dispose();

            RabbitMQPippoPazzoSubscriber m_tsb2 = new RabbitMQPippoPazzoSubscriber();
            m_tsb2.Init();
        }

        private static void ScheduleMessageTest()
        {
            MessaggioTest m_test1 = new MessaggioTest();
            m_test1.Name = "pippo 101";
            m_test1.Age = 402;

            MessaggioTest m_test2 = new MessaggioTest();
            m_test2.Name = "pippo 102";
            m_test2.Age = 4023;



            //
            GrayParrot.Scheduling.Scheduler m_scheduler = new GrayParrot.Scheduling.Scheduler();
            m_scheduler.Initialize();

            JobParameters m_parameters = new JobParameters(m_test1);
            m_parameters.Parameters.Add("subscription", "GrayParrot-Test");

            m_scheduler.SetSchedule<GrayParrot.RabbitMQ.ScheduledMessage<MessaggioTest>>(DateTime.Now.AddSeconds(30), m_parameters);

            //
            JobParameters m_parameters2 = new JobParameters(m_test2);
            m_parameters2.Parameters.Add("subscription", "PippoPazzo-Test");

            m_scheduler.SetSchedule<GrayParrot.RabbitMQ.ScheduledMessage<MessaggioTest>>(DateTime.Now.AddMinutes(1), m_parameters2);
        }
    }
}
